package com;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 *  JMenuBar personalizada que permite manejar eventos de una ventana
 * 
 * @author Alex Muñoz   -   Mauricio Orellana.
 * @version 2012-07-13
 */
public class MenuBar extends JMenuBar implements ActionListener {
    
    private Window window;
    
    /**
     * Contructor de Objetos de la clase MenuBar
     * 
     * @param window Ventna que contiene a la MenuBar 
     */
    public MenuBar(Window window)
    {
        this.window = window;
        
        JMenu menuTetravex = new JMenu(CommandNames.MENU_PLAY);
        menuTetravex.setMnemonic( CommandNames.MENU_PLAY_MNEMONIC );
        super.add(menuTetravex);
        
        this.agregarMenuItem(menuTetravex, CommandNames.MENU_ITEM_NEW, CommandNames.MENU_ITEM_NEW_MNEMONIC);
        this.agregarMenuItem(menuTetravex, CommandNames.MENU_ITEM_SCORE, CommandNames.MENU_ITEM_SCORE_MNEMONIC);
        menuTetravex.addSeparator();
        
        JMenu menuTamanio = new JMenu(CommandNames.MENU_ITEM_SIZE);
        this.agregarMenuItem(menuTamanio,CommandNames.ROW_COLUMN_2X2, CommandNames.ROW_COLUMN_2X2_MNEMONIC);
        this.agregarMenuItem(menuTamanio,CommandNames.ROW_COLUMN_3X3, CommandNames.ROW_COLUMN_3X3_MNEMONIC);
        this.agregarMenuItem(menuTamanio,CommandNames.ROW_COLUMN_4X4, CommandNames.ROW_COLUMN_4X4_MNEMONIC);
        this.agregarMenuItem(menuTamanio,CommandNames.ROW_COLUMN_5X5, CommandNames.ROW_COLUMN_5X5_MNEMONIC);
        this.agregarMenuItem(menuTamanio,CommandNames.ROW_COLUMN_6X6, CommandNames.ROW_COLUMN_6X6_MNEMONIC);
        
        menuTetravex.add(menuTamanio);
       
        this.agregarMenuItem(menuTetravex,CommandNames.MENU_ITEM_EXIT, CommandNames.MENU_ITEM_EXIT_NMEMONIC);
        
        JMenu menuAbout = new JMenu(CommandNames.MENU_HELP);
        menuAbout.setMnemonic( CommandNames.MENU_HELP_NMEMONIC );
        this.agregarMenuItem(menuAbout, CommandNames.MENU_ITEM_ABOUT, CommandNames.MENU_ITEM_ABOUT_NMEMONIC);
        super.add(menuAbout);    
    }
    /**
     * Permite simplificar el proceso de agregar menu Items a un Menu
     * 
     * @param parent JMenu al que se agregaran los JMenuItem
     * @param caption Cadena a mostrar en el JMenuItem
     * @param mnemonic Atajo que posee el JMenuItem
     */
    public void agregarMenuItem( JMenu parent, String caption, char mnemonic )
    {
        JMenuItem item = new JMenuItem(caption);
        item.setMnemonic(mnemonic);
        item.setActionCommand(caption);
        item.addActionListener(this);
        parent.add(item);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {

        switch(ae.getActionCommand())
        {
            case "About":
                         
                         Dialog dig = new Dialog("About");
                         dig.setcontenido(new About());
                         dig.setLocationRelativeTo(this.window);
                         dig.setVisible(true);
                    break;
            case "Nuevo":
                        Window w = new Window();
                        this.window.dispose();
                        w.setVisible(true);
                        this.window = w;
                        break;
            case "2x2":
                        window.dispose();
                        window = new Window();
                        window.setSize(new Dimension(window.getArena().getSizeM()*2 * 100 + 200,window.getArena().getSizeM()*100+150));
                        window.setLocationRelativeTo(null);
                        window.setVisible(true);
                        break;
            case "3x3":
                        window.dispose();
                        window = new Window(new ArenaDeJuego(3));
                        window.setSize(new Dimension(window.getArena().getSizeM()*2 * 100 + 200,window.getArena().getSizeM()*100+150));
                        window.setLocationRelativeTo(null);
                        window.setVisible(true); 
                        break;
            case "4x4":
                        window.dispose();
                        window = new Window(new ArenaDeJuego(4));
                        window.setSize(new Dimension(window.getArena().getSizeM()*2 * 100 + 200,window.getArena().getSizeM()*100+150));
                        window.setLocationRelativeTo(null);
                        window.setVisible(true); 
                        break;
            case "5x5":
                        window.dispose();
                        window = new Window(new ArenaDeJuego(5));
                        window.setSize(new Dimension(window.getArena().getSizeM()*2 * 100 + 200,window.getArena().getSizeM()*100+150));
                        window.setLocationRelativeTo(null);
                        window.setVisible(true); 
                        break;
            case "6x6":
                        window.dispose();
                        window = new Window(new ArenaDeJuego(6));
                        window.setSize(new Dimension(window.getArena().getSizeM()*2 * 100 + 200,window.getArena().getSizeM()*100+150));
                        window.setLocationRelativeTo(null);
                        window.setVisible(true); 
                        break; 
                
            case "Puntuacion":
                         Dialog d = new Dialog("Score");
                         Score s = new Score();
                         d.setcontenido(s);
                         d.setLocationRelativeTo(this.window);
                         d.setVisible(true);
                         break;
        }
    }
}
